/**
 * Copyright (c) 2006, Reliable Source, Inc. 
 * All Rights Reserved.
 */
package org.reliablesource.prism.ui.workbench.views;

import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IPartListener2;
import org.eclipse.ui.IPartService;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchPartReference;
import org.eclipse.ui.part.ViewPart;
import org.reliablesource.prism.ui.plugin.PrismPlugin;
import org.reliablesource.prism.ui.viewers.AnimationViewer;
import org.reliablesource.prism.ui.viewers.ViewerCreateControlException;
import org.reliablesource.prism.ui.viewers.ViewerInitException;

/**
 * AnimationViewPart.java<br> 
 * Created on: May 10, 2006<br>
 * 
 * @author Dennis Park 
 * 		<a href="mailto:dennis.park@gmail.com">dennis.park@gmail.com</a>
 * 
 *  @since 1.0
 *  
 */

public class AnimationViewPart extends ViewPart implements IPartListener2 {
	public final static String VIEW_ID = "org.reliablesource.prism.ui.views.workbench.animationview";

	protected AnimationViewer animationViewer;
	protected boolean isPaused  = false;

	public AnimationViewPart() {
		super();
	}

	public void createPartControl(final Composite parent) {

		animationViewer = new AnimationViewer();
		try {
			animationViewer.init();
			animationViewer.createViewerControl(parent);
		} catch (ViewerInitException e1) {
			e1.printStackTrace();
		} catch (ViewerCreateControlException e) {
			e.printStackTrace();
		}

		// Listen only to selection in the Navigator.
		// getSite().getPage().addSelectionListener(NavigatorViewPart.VIEW_ID,
		// animationViewer);

		// This is too responsive for code/debug impl. stage. Should change to
		// a checkbox tree viewer for selection.

		PrismPlugin.getDefault().addWidget(VIEW_ID, parent);
		PrismPlugin.getDefault().dumpCompositeContextMap();
	}

	public void setFocus() {
		if (animationViewer != null)
			if (animationViewer.getControl() != null)
				animationViewer.getControl().setFocus();
	}

	public void dispose() {

		if (animationViewer != null) {
			animationViewer.dispose();
			animationViewer = null;
		}

		super.dispose();
	}

	public Viewer getContentViewer() {
		return animationViewer;
	}

	public void partActivated(IWorkbenchPart part) {
	}

	public void partBroughtToTop(IWorkbenchPart part) {
	}

	public void partClosed(IWorkbenchPart part) {
	}

	public void partDeactivated(IWorkbenchPart part) {
	}

	public void partOpened(IWorkbenchPart part) {
		if(!part.equals(this))
			return;
		
		IPartService _ptSvc = (IPartService) getSite().getService(IPartService.class);
		_ptSvc.addPartListener(this);
	}

	public void partActivated(IWorkbenchPartReference partRef) {
	}

	public void partBroughtToTop(IWorkbenchPartReference partRef) {
	}

	public void partClosed(IWorkbenchPartReference partRef) {
		if(!partRef.getPart(false).equals(this))
			return;
		IPartService _ptSvc = (IPartService) getSite().getService(IPartService.class);
		_ptSvc.addPartListener(this);
	}

	public void partDeactivated(IWorkbenchPartReference partRef) {
	}

	public void partHidden(IWorkbenchPartReference partRef) {
		if(!partRef.getPart(false).equals(this))
			return;
		
		if(animationViewer == null)
			return;
		
		animationViewer.pauseAnimation();
	}

	public void partVisible(final IWorkbenchPartReference partRef) {
		if(!partRef.getPart(false).equals(this))
			return;
		partRef.getPart(false).equals(this);
		
		if(animationViewer == null)
			return;
		
		animationViewer.playAnimation();
	}
	
	public void partInputChanged(IWorkbenchPartReference partRef) {
	}

	public void partOpened(IWorkbenchPartReference partRef) {
	}

}
